import { createMistake } from '@/api/mistake'

const state = {
  logs: []
}

const mutations = {
  ADD_ERROR_LOG: (state, log) => {
    state.logs.push(log)
  },
  CLEAR_ERROR_LOG: (state) => {
    state.logs.splice(0)
  }
}

const actions = {
  addErrorLog({ commit }, log) {
    return new Promise((resolve, reject) => {
      createMistake({
        name: log.err.message,
        info: log.info,
        url: log.url,
        source: 'web'
      })
      commit('ADD_ERROR_LOG', log)
      resolve()
    })
  },
  clearErrorLog({ commit }) {
    commit('CLEAR_ERROR_LOG')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
